import 'package:flutter/material.dart';
import '../../utils/base.dart';
import '../../beans/device_bean.dart';

class DeviceItem extends StatelessWidget {
  final DeviceBean data;
  const DeviceItem(this.data, {Key key}) : super(key: key);

  Widget _header() {
    return Container(
      decoration: BoxDecoration(
        border: Border(
            bottom: BorderSide(width: Design.px(1), color: HexColor('d5d5d5'))),
      ),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: Design.px(25)),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Container(
                child: Text(
                  data.shopName,
                  style: TextStyle(
                    fontSize: Design.px(25),
                    color: HexColor('999999'),
                    height: 1.5
                  ),
                ),
              ),
            ),
            Text(
              data.deviceTypeName,
              style: TextStyle(
                fontSize: Design.px(25),
                color: HexColor('999999'),
                height: 1.5
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _body() {
    Widget _deviceSn = Text(
      data.deviceSn,
      style: TextStyle(
        color: HexColor('333333'),
        fontSize: Design.px(34),
        height: 2,
      ),
    );

    Widget _statsText(String text) {
      return Text(
        text,
        style: TextStyle(
          fontSize: Design.px(26),
          color: HexColor('666666'),
          height: 1.5,
        ),
      );
    }

    Widget _statusText() {
      Color _textColor;
      String _statusText;
      if (data.status == 1) {
        _statusText = '云采器离线';
        _textColor = HexColor('666666');
      } else if (data.status == 2) {
        _statusText = '云采器在线';
        _textColor = HexColor('3ec270');
      }
      return Text(
        _statusText,
        style: TextStyle(
          fontSize: Design.px(26),
          color: _textColor,
          height: 2,
        ),
      );
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: Design.px(25)),
      decoration: BoxDecoration(
        border: Border(
            bottom: BorderSide(width: Design.px(1), color: HexColor('d5d5d5'))),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                _deviceSn,
                _statsText('总上分：${data.totalCoin}'),
                _statsText('总下分：${data.totalPoint}'),
              ],
            ),
          ),
          _statusText(),
        ],
      ),
    );
  }

  Widget _footer() {
    Widget _flatButton(String text, VoidCallback onPressed) {
      return Container(
        height: Design.px(58),
        margin: EdgeInsets.only(right: Design.px(29)),
        decoration: BoxDecoration(
          border: Border.all(width: Design.px(1), color: HexColor('ffa26c')),
          borderRadius: BorderRadius.circular(Design.px(58)),
        ),
        child: FlatButton(
          onPressed: onPressed,
          child: Text(
            text,
            style: TextStyle(
              fontSize: Design.px(30),
              color: HexColor('ff7920'),
            ),
          ),
        ),
      );
    }

    Widget _statusButton(String text, VoidCallback onPressed) {
      return Container(
        height: Design.px(58),
        width: Design.px(170),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(Design.px(58)),
          gradient: LinearGradient(
            colors: [HexColor('ff91a2'),HexColor('ff6a75'),]
          )
        ),
        child: FlatButton(
          onPressed: onPressed,
          child: Text(text, style: TextStyle(
            color: Colors.white,
            fontSize: Design.px(30),
          ),),
        ),
      );
    }

    return Container(
      padding: EdgeInsets.symmetric(
          horizontal: Design.px(25), vertical: Design.px(28)),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Row(
              children: <Widget>[
                _flatButton('查看下分', () => {}),
                _flatButton('查看上分', () => {}),
              ],
            ),
          ),
          _statusButton('停用', () => {}),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
          horizontal: Design.px(25), vertical: Design.px(10)),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(Design.px(8)),
      ),
      child: Column(
        children: <Widget>[
          _header(),
          _body(),
          _footer(),
        ],
      ),
    );
  }
}
